package com.by.yz.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.by.yz.App;
import com.by.yz.R;
import com.by.yz.activity.AddFriendActivity;
import com.by.yz.activity.CurrentTopicsActivity;
import com.by.yz.custom.CircleNetworkImageView;
import com.by.yz.entity.Account;
import com.by.yz.entity.Click;
import com.by.yz.entity.TopicInfo;
import com.by.yz.entity.UserTest;
import com.by.yz.loginUtil.AccountRESTFul;
import com.by.yz.loginUtil.ClickRESTtful;
import com.by.yz.loginUtil.TopicInfoRESTFul;
import com.by.yz.loginUtil.UserRESTFul;
import com.by.yz.util.Start_End_times;
import com.by.yz.util.ToJSonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by mrs yang on 2016/10/27.
 */
public class TopicItemAdapter extends RecyclerView.Adapter<TopicItemAdapter.ViewHolder>{
		private final TypedValue mTypedValue = new TypedValue();
		private int mBackground;
		private List<TopicInfo> mValues;
	    private Start_End_times set;
		private UserTest userTest;
		private Account account;
		Click click;
		Integer Dcount;//点赞数
		Context con;

		public static class ViewHolder extends RecyclerView.ViewHolder {
			public TopicInfo tt;
			public final View mView;
			public final NetworkImageView topicimage1,topicimage2,topicimage3;
			public final TextView hot_topic_title;
			public final TextView hot_topic_content;
			public final TextView report_time;
			public final CircleNetworkImageView headimg;
			public final TextView reporter;
			public final TextView dianzan_count;
			public final TextView comment_count;
			public final TextView communityname;
			public final RelativeLayout comment_layout;
			public final RelativeLayout dianzan_layout;
			final ImageView dianzan;

			public ViewHolder(View view) {
				super(view);
				mView = view;
				topicimage1 = (NetworkImageView) view.findViewById(R.id.topicimage1);
				topicimage2 = (NetworkImageView) view.findViewById(R.id.topicimage2);
				topicimage3 = (NetworkImageView) view.findViewById(R.id.topicimage3);
				hot_topic_title = (TextView) view.findViewById(R.id.hot_topic_title);
				hot_topic_content = (TextView) view.findViewById(R.id.hot_topic_content);
				report_time = (TextView) view.findViewById(R.id.report_time_textview);
				headimg=(CircleNetworkImageView) view.findViewById(R.id.report_person_head_image);
				reporter = (TextView) view.findViewById(R.id.report_person_name_textview);
				dianzan_count =(TextView) view.findViewById(R.id.dianzan_count_textview);
				comment_count =(TextView) view.findViewById(R.id.comment_count_textview);
				communityname=(TextView)view.findViewById(R.id.communicaty_textview);
				dianzan=(ImageView)view.findViewById(R.id.dianzan_image);
				comment_layout=(RelativeLayout)view.findViewById(R.id.comment_layout);
				dianzan_layout=(RelativeLayout)view.findViewById(R.id.dianzan_layout);
			}
		}

		public TopicInfo getTitle(int position) {
			return mValues.get(position);
		}

		public TopicItemAdapter(Context context, List<TopicInfo> items) {
			context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
			mBackground = mTypedValue.resourceId;
			mValues = items;
		}

		@Override
		public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
			View view = LayoutInflater.from(parent.getContext())
					.inflate(R.layout.communicaty_hot_topic_items, parent, false);
			return new ViewHolder(view);
		}

		@Override
		public void onBindViewHolder(final ViewHolder holder, final int position) {
			holder.tt = mValues.get(position);
			holder.hot_topic_title.setText(mValues.get(position).getTitle());
			AccountRESTFul accountRESTFul=new AccountRESTFul();
			accountRESTFul.get(new AccountRESTFul.IhttpCb() {
				@Override
				public void ok(JSONObject response) {
					JSONObject jsonObject=response;
					account=ToJSonObject.jsonToBean(jsonObject.toString(),Account.class);
					ClickRESTtful clickRESTtful = new ClickRESTtful();
					clickRESTtful.getTopic(mValues.get(position).getId(), new ClickRESTtful.MyGetCallBack() {
						@Override
						public void ok(JSONArray response) throws JSONException {
							if (response.length()==0){
								holder.dianzan.setImageResource(R.mipmap.community_sign_up_normal);
							}else {
								for (int i = 0; i < response.length(); i++) {
									click = new Click();
									JSONObject jsonObject = response.getJSONObject(i);
									click = ToJSonObject.jsonToBean(jsonObject.toString(), Click.class);
									if (click.getTopic().equals(mValues.get(position).getId()) && click.getClicker().equals(account.getLogin())) {
										holder.dianzan.setImageResource(R.mipmap.community_sign_up_press);
										holder.dianzan.setClickable(false);
									}
								}
							}
						}
						@Override
						public void error(String error) {
							holder.dianzan.setImageResource(R.mipmap.community_sign_up_normal);
						}
					});
				}
				@Override
				public void error(String error) {
					Log.e("account-error","授权失败"+error);
				}
			});
			holder.hot_topic_content.setText(mValues.get(position).getContent());
			String time=mValues.get(position).getCreateat();
			String time1=time.replace("T"," ");
			String time2=time1.substring(0,19);
			set=new Start_End_times(time2);
			holder.report_time.setText(set.str_date());
			holder.headimg.setDefaultImageResId(R.mipmap.headimg);
			holder.headimg.setErrorImageResId(R.mipmap.headimg_error);
			UserRESTFul userRESTFul=new UserRESTFul();
			userRESTFul.getUser(mValues.get(position).getCreater(), new UserRESTFul.UserPOSTCallBack() {
				@Override
				public void ok(JSONObject response) throws JSONException {
					userTest = ToJSonObject.jsonToBean(response.toString(), UserTest.class);
					holder.reporter.setText(userTest.getnickName());
					if (userTest.getHeadimg()!=null){
//						Glide.with(holder.headimg.getContext())
//								.load(userTest.getHeadimg())
//								.fitCenter()
//								.into(holder.headimg);
						holder.headimg.setImageUrl(userTest.getHeadimg(), App.imageLoader);
					}
				}
				@Override
				public void error(String response) {}
			});
			//holder.reporter.setText(mValues.get(position).getCreater());
			Integer dcount=mValues.get(position).getCount();
			Integer ccount=mValues.get(position).getDisscusscount();
			holder.dianzan_count.setText(dcount.toString());
			holder.comment_count.setText(ccount.toString());
			holder.communityname.setText(mValues.get(position).getComunity());
			holder.dianzan.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					con = v.getContext();
					click = new Click();
					click.setClicker(account.getLogin());
					click.setIsclick(true);
					click.setTopic(mValues.get(position).getId());
					JSONObject jsonObject = ToJSonObject.beanToJSONObejct(click);
					ClickRESTtful clickRESTtful = new ClickRESTtful();
					clickRESTtful.postClick(jsonObject, new ClickRESTtful.MyPostCallBack() {
						@Override
						public void ok(JSONObject response) throws JSONException {
							Log.e("click-response", response.toString());
							Dcount = holder.tt.getCount()+1;
							Log.e("Dcount = ",Dcount+"");
							holder.dianzan.setImageResource(R.mipmap.community_sign_up_press);
							holder.dianzan_count.setText(Dcount+"");
							holder.tt.setCount(Dcount);
							JSONObject jsonObject = ToJSonObject.beanToJSONObejct(holder.tt);
							TopicInfoRESTFul topicful = new TopicInfoRESTFul();
							topicful.putTopicInfo(jsonObject, new TopicInfoRESTFul.MyPostCallBack() {
								@Override
								public void ok(JSONObject response) throws JSONException {
									Log.e("topInfo-response", response.toString());
									holder.dianzan.setClickable(false);
									Toast.makeText(con, "点赞成功！", Toast.LENGTH_SHORT).show();
									Log.e("成功", "点赞成功");
								}
								@Override
								public void error(String error) {
									Log.e("失败", "点赞失败" + error);
								}
							});
						}
						@Override
						public void error(String error) {
							Log.e("eeeee", "点赞失败" + error);
						}
					});
				}
			});
			holder.comment_layout.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Context context = v.getContext();
					Intent intent = new Intent(context, CurrentTopicsActivity.class);
					Bundle bundle = new Bundle();
					bundle.putSerializable("currentopic",holder.tt);
					intent.putExtras(bundle);
					intent.putExtra("isComment",true);
					context.startActivity(intent);
				}
			});
			holder.mView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Context context = v.getContext();
					Intent intent = new Intent(context, CurrentTopicsActivity.class);
					Bundle bundle = new Bundle();
					bundle.putSerializable("currentopic",holder.tt);
					intent.putExtras(bundle);
					intent.putExtra("isComment",false);
					context.startActivity(intent);
				}
			});
			holder.headimg.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					Context context = view.getContext();
					Intent intent = new Intent(context, AddFriendActivity.class);
					Bundle bundle = new Bundle();
					bundle.putSerializable("currentopics",mValues.get(position).getCreater());
					intent.putExtras(bundle);
					context.startActivity(intent);
				}
			});
			String topicimagepath=mValues.get(position).getTopicimage();
			if (topicimagepath!=null && !topicimagepath.equals("")) {
				String[] imagepath = topicimagepath.split(",");
				if (imagepath.length==1){
					holder.topicimage1.setDefaultImageResId(R.mipmap.health_passage_default);
					holder.topicimage1.setErrorImageResId(R.mipmap.health_topic_error);
					holder.topicimage1.setImageUrl(imagepath[0].toString(), App.imageLoader);
//					Glide.with(holder.topicimage1.getContext())
//							.load(mValues.get(position).getTopicimage())
//							.fitCenter()
//							.into(holder.topicimage1);
					holder.topicimage2.setVisibility(View.INVISIBLE);
					holder.topicimage3.setVisibility(View.INVISIBLE);
				}else if (imagepath.length == 2){
					holder.topicimage1.setDefaultImageResId(R.mipmap.health_passage_default);
					holder.topicimage1.setErrorImageResId(R.mipmap.health_topic_error);
					holder.topicimage1.setImageUrl(imagepath[0].toString(), App.imageLoader);
					holder.topicimage2.setDefaultImageResId(R.mipmap.health_passage_default);
					holder.topicimage2.setErrorImageResId(R.mipmap.health_topic_error);
					holder.topicimage2.setImageUrl(imagepath[1].toString(), App.imageLoader);
//					Glide.with(holder.topicimage1.getContext())
//							.load(mValues.get(position).getTopicimage())
//							.fitCenter()
//							.into(holder.topicimage1);
//					Glide.with(holder.topicimage2.getContext())
//							.load(mValues.get(position).getTopicimage())
//							.fitCenter()
//							.into(holder.topicimage2);
					holder.topicimage3.setVisibility(View.INVISIBLE);
				}
				else if(imagepath.length == 3) {
					holder.topicimage1.setDefaultImageResId(R.mipmap.health_passage_default);
					holder.topicimage1.setErrorImageResId(R.mipmap.health_topic_error);
					holder.topicimage1.setImageUrl(imagepath[0].toString(), App.imageLoader);
					holder.topicimage2.setDefaultImageResId(R.mipmap.health_passage_default);
					holder.topicimage2.setErrorImageResId(R.mipmap.health_topic_error);
					holder.topicimage2.setImageUrl(imagepath[1].toString(), App.imageLoader);
					holder.topicimage3.setDefaultImageResId(R.mipmap.health_passage_default);
					holder.topicimage3.setErrorImageResId(R.mipmap.health_topic_error);
					holder.topicimage3.setImageUrl(imagepath[2].toString(), App.imageLoader);
//					Glide.with(holder.topicimage1.getContext())
//							.load(mValues.get(position).getTopicimage())
//							.fitCenter()
//							.into(holder.topicimage1);
//					Glide.with(holder.topicimage2.getContext())
//							.load(mValues.get(position).getTopicimage())
//							.fitCenter()
//							.into(holder.topicimage2);
//					Glide.with(holder.topicimage3.getContext())
//							.load(mValues.get(position).getTopicimage())
//							.fitCenter()
//							.into(holder.topicimage3);
				}
			}else{
				holder.topicimage1.setVisibility(View.GONE);
				holder.topicimage2.setVisibility(View.GONE);
				holder.topicimage3.setVisibility(View.GONE);
			}

		}

		@Override
		public int getItemCount() {
			return mValues.size();
		}
}
